{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1的值为：\n",
      " [[0.97974888 0.38731184 0.21962602 0.10827049]\n",
      " [0.97100491 0.24536715 0.3831528  0.40753956]\n",
      " [0.5696705  0.05421515 0.50740575 0.17071737]]\n",
      "np.reshape的结果：\n",
      " [[0.97974888 0.24536715 0.50740575]\n",
      " [0.97100491 0.05421515 0.10827049]\n",
      " [0.5696705  0.21962602 0.40753956]\n",
      " [0.38731184 0.3831528  0.17071737]]\n",
      "arr1的值为：\n",
      " [[0.97974888 0.38731184 0.21962602 0.10827049]\n",
      " [0.97100491 0.24536715 0.3831528  0.40753956]\n",
      " [0.5696705  0.05421515 0.50740575 0.17071737]]\n",
      "arr1内置reshape的结果：\n",
      " [[0.97974888 0.38731184 0.21962602]\n",
      " [0.10827049 0.97100491 0.24536715]\n",
      " [0.3831528  0.40753956 0.5696705 ]\n",
      " [0.05421515 0.50740575 0.17071737]]\n",
      "arr1的尺寸推断\n",
      " [[0.97974888 0.38731184 0.21962602]\n",
      " [0.10827049 0.97100491 0.24536715]\n",
      " [0.3831528  0.40753956 0.5696705 ]\n",
      " [0.05421515 0.50740575 0.17071737]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.random.rand(3, 4)\n",
    "print(\"arr1的值为：\\n\", arr1)\n",
    "print(\"np.reshape的结果：\\n\", np.reshape(arr1, newshape=(4, 3), order='F'))\n",
    "print(\"arr1的值为：\\n\", arr1)\n",
    "print(\"arr1内置reshape的结果：\\n\", arr1.reshape(4, 3))\n",
    "print(\"arr1的尺寸推断\\n\", arr1.reshape(4, -1))\n",
    "# print(\"使用不恰当的尺寸：\\n\", arr1.reshape(3, 5))?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a的数值为：\n",
      " [[0 1]\n",
      " [2 3]]\n",
      "adarry.resize的结果：\n",
      " [[0 1]\n",
      " [2 3]]\n",
      "np.resize的结果：\n",
      " [[0]\n",
      " [1]]\n",
      "a的数值为：\n",
      " [[0 1]\n",
      " [2 3]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[0, 1], [2, 3]])\n",
    "print(\"a的数值为：\\n\", a)\n",
    "# a.resize(2, 1)\n",
    "print(\"adarry.resize的结果：\\n\", a) # 可以忽略size的匹配 直接改变a 返回值为None\n",
    "print(\"np.resize的结果：\\n\", np.resize(a, new_shape=(2, 1)))    # 可以忽略size的匹配 不改变a 返回值是变换的结果\n",
    "print(\"a的数值为：\\n\", a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# np.reshape\n",
    "使用np.reshape函数将形状为(4,3)的Numpy数组重新按行整形为(6,2)的形状，并打印结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4行3列数组：\n",
      " [[0.70562216 0.80659619 0.95465507]\n",
      " [0.57373483 0.34423591 0.22620869]\n",
      " [0.12899063 0.30246935 0.67982186]\n",
      " [0.38429513 0.93710351 0.70780042]]\n",
      "整形后的数组：\n",
      " [[0.70562216 0.80659619]\n",
      " [0.95465507 0.57373483]\n",
      " [0.34423591 0.22620869]\n",
      " [0.12899063 0.30246935]\n",
      " [0.67982186 0.38429513]\n",
      " [0.93710351 0.70780042]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.random(size=(4, 3))\n",
    "print(\"4行3列数组：\\n\", arr)\n",
    "reshaped_arr = np.reshape(a=arr, newshape=(6, 2), order='C')\n",
    "print(\"整形后的数组：\\n\", reshaped_arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ndarray.reshape\n",
    "创建一个形状为(2,3)的Numpy数组，并使用ndarray.reshape方法将其重新整形为(3,2)的形状，同时指定按列进行整形，并打印结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始的数组：\n",
      " [[-1.50441827  1.22181937  0.17230738]\n",
      " [-2.61904079  2.25409249  0.20768031]]\n",
      "按列整形的数组：\n",
      " [[-1.50441827  2.25409249]\n",
      " [-2.61904079  0.17230738]\n",
      " [ 1.22181937  0.20768031]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.normal(loc=1, scale=3, size=(2, 3))\n",
    "print(\"原始的数组：\\n\", arr)\n",
    "reshaped_arr = arr.reshape(3, 2, order='F')\n",
    "print(\"按列整形的数组：\\n\", reshaped_arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# np.resize\n",
    "给定一个形状为(2,2)的Numpy数组，请使用np.resize函数将其重新整形为(3,3)的形状，并打印结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组为：\n",
      " [[10.07872177 14.50200548]\n",
      " [11.95162171 12.19076985]]\n",
      "resize后的数组为：\n",
      " [[10.07872177 14.50200548 11.95162171]\n",
      " [12.19076985 10.07872177 14.50200548]\n",
      " [11.95162171 12.19076985 10.07872177]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.uniform(low=10, high=20, size=(2, 2))\n",
    "print(\"原始数组为：\\n\", arr)\n",
    "reshaped_arr = np.resize(arr, new_shape=(3, 3))\n",
    "print(\"resize后的数组为：\\n\", reshaped_arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ndarray.resize\n",
    "创建一个形状为(2,2)的Numpy数组，并使用ndarray.resize方法将其就地重新整形为(2,3)的形状，并打印原数组和新数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组为：\n",
      " [[87 27]\n",
      " [52 98]]\n",
      "resized后的数组为：\n",
      " [[87 27 52]\n",
      " [98  0  0]]\n",
      "返回值为：\n",
      " None\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.randint(low=0, high=100, size=(2, 2))\n",
    "print(\"原始数组为：\\n\", arr)\n",
    "result = arr.resize((2, 3))\n",
    "print(\"resized后的数组为：\\n\", arr) # 0填充\n",
    "print(\"返回值为：\\n\", result)   # 返回值为None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组转置\n",
    "给定一个形状为(3,4)的Numpy数组，请使用.T属性将其转置，并打印结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组为：\n",
      " [[0.60595217 0.5531711  0.13218985 0.84656717]\n",
      " [0.9690352  0.92773567 0.85932838 0.24209199]\n",
      " [0.38377904 0.89634589 0.17422479 0.61047258]]\n",
      "转置后的数组为：\n",
      " [[0.60595217 0.9690352  0.38377904]\n",
      " [0.5531711  0.92773567 0.89634589]\n",
      " [0.13218985 0.85932838 0.17422479]\n",
      " [0.84656717 0.24209199 0.61047258]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.random(size=(3, 4))\n",
    "print(\"原始数组为：\\n\", arr)\n",
    "transposed_arr = arr.T\n",
    "print(\"转置后的数组为：\\n\", transposed_arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ndarray.astype\n",
    "创建一个形状为(3,4)的Numpy数组，其元素为浮点数，并使用ndarray.astype方法将其转换为整数类型，并打印转换后的数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "初始化数组：\n",
      " [[ 0.75163566  0.3702788  -0.01433911  1.40446001]\n",
      " [ 0.67142795  0.91387125 -0.38835781  0.06833514]\n",
      " [-0.17156185 -0.78636709  0.20241878 -1.05823882]]\n",
      "初始化数组类型：\n",
      " float64\n",
      "变更类型后的数组：\n",
      " [[ 0  0  0  1]\n",
      " [ 0  0  0  0]\n",
      " [ 0  0  0 -1]]\n",
      "变更后的数组类型：\n",
      " int32\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.randn(3, 4)\n",
    "print(\"初始化数组：\\n\", arr)\n",
    "print(\"初始化数组类型：\\n\", arr.dtype)\n",
    "new_arr = arr.astype(np.int32)\n",
    "print(\"变更类型后的数组：\\n\", new_arr)\n",
    "print(\"变更后的数组类型：\\n\", new_arr.dtype)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
